package com.baizhi.service.impl;

import com.baizhi.dao.GroupDAO;
import com.baizhi.dto.GroupsDTO;
import com.baizhi.entity.Group;
import com.baizhi.service.GroupService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class GroupServiceImpl extends ServiceImpl<GroupDAO, Group> implements GroupService {

    @Autowired
    private GroupDAO groupDAO;

    @Override
    public List<Group> queryAll() {
        List<Group> groups = groupDAO.selectList(null);
        return groups;
    }

    @Override
    public Group add(GroupsDTO groupsDTO) {
        Group entity = new Group();
        entity.setName(groupsDTO.getName());
        Long clazzId = groupsDTO.getClazzId();
        entity.setClazzId(clazzId);
        Date createdate = new Date();
        entity.setCreatedate(createdate);
        int insert = groupDAO.insert(entity);
        //根据名字查
        LambdaQueryWrapper<Group> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Group::getName,groupsDTO.getName());
        queryWrapper.eq(Group::getClazzId,clazzId);
        Group group = groupDAO.selectOne(queryWrapper);
        return group;
    }

    @Override
    public Group queryById(Long id) {
        LambdaQueryWrapper<Group> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Group::getId,id);
        Group group = groupDAO.selectOne(queryWrapper);
        return group;
    }
}
